package 多线程;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2017/3/28.
 */
public class myRunnale2 {
    public static void main(String[] args) {
        List s =new ArrayList<>();
        System.out.println("test");
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        System.out.println("可用线程数量：" + availableProcessors);
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(availableProcessors);
        for (int i = 0; i < 10; i++) {
            BusienssRunnable busienssRunnable = new BusienssRunnable(s,i);

            fixedThreadPool.execute(busienssRunnable);
        }
		/*
		 * shutdown()启动一次顺序关闭，执行以前提交的任务，但不接受新任务。如果已经关闭，则调用没有其他作用。
		 */
        fixedThreadPool.shutdown();
       while (true) {
             //判断是否所有的线程已经运行完
            boolean terminated = fixedThreadPool.isTerminated();
            // System.out.println("terminated:" + terminated);
            if (terminated) {
                System.out.printf(s.toString());
                System.out.println("All is finished!");
                System.exit(0);//退出*//*
            }
        }
    }
}

class BusienssRunnable implements Runnable {
    private int i;
    private List s;
    public BusienssRunnable(List s,int i) {
        this.i = i;
        this.s = s;
    }

    @Override
    public void run() {
        s.add(i);
    }

}
